package hello;
import java.io.*;
import javax.microedition.io.CommConnection;
import javax.microedition.io.Connector;

import javax.microedition.lcdui.*;

public class UploadJob implements Runnable
{
    HelloMidlet midlet;  
    int gaugeValue;
    int gaugeLimit;
    final int increase = 2048;
    Gauge gauge;
    boolean backGround = false;
    Command okCommand;
   
    
    public UploadJob( HelloMidlet m)
    {
        midlet = m;
        midlet.uploading = true;            
        Thread t = new Thread(this);
        t.start();        
        
        gaugeLimit = midlet.baos.size();
        gaugeValue = 0;
        ////#style BusyScreen
        Form busyForm = new Form("");        
        okCommand = new Command("Ok", Command.ITEM, 1);        
        gauge = new Gauge("Uploading (Bytes)...", false, gaugeLimit, 0);                   
        busyForm.append(gauge);
        busyForm.append("Press Ok to push uploading to the background...");
        midlet.getDisplay().setCurrent(busyForm);                               
        
        try {
            Thread.sleep(5000);
        } catch (Exception e) {
            
        }
                
        busyForm.addCommand(okCommand);
        busyForm.setCommandListener(new CommandListener() {
            public void commandAction(Command command, Displayable displayable) 
            {
                if(okCommand == command)
                {
                    backGround = true;
                    midlet.getDisplay().setCurrent(midlet.menuScreen);           
                }
            }
        });
        
        new Thread(this)
        {
            public void run()
            {
                while(midlet.uploading && backGround == false) 
                {
                    try{
                        Thread.sleep(300);
                        int temp = increase + gaugeValue;
                        if(temp < gaugeLimit)
                        {
                            gaugeValue = temp;
                            gauge.setValue(gaugeValue);
                        }
                    }
                    catch(Exception e)
                    {}
                }
            }
        }.start();
    }


    public void run()
    {                   
        CommConnection c = null;        
        DataOutputStream os = null;
        
        try
        {                    

            c = (CommConnection)Connector.open("comm:COM0");
            os = c.openDataOutputStream();                                                          
            
            os.write(midlet.baos.toByteArray());             
            os.flush();
            midlet.baos = null;             
           
            Alert alert1 = new Alert("", "Notification:\n\nUploading finished", null, AlertType.INFO);
            alert1.setTimeout(5000);        
            midlet.getDisplay().setCurrent(alert1);
           
        }
        catch (Exception e)
        {
            Alert alert1 = new Alert("", "Notification:\n\nUploading failed...Try again later", null, AlertType.INFO);
            alert1.setTimeout(5000);        
            midlet.getDisplay().setCurrent(alert1);            
        }
        finally
        {   
            midlet.uploading = false;    
            if(!backGround)
            {
                 midlet.getDisplay().setCurrent(midlet.menuScreen);           
            }
            if (os != null)
            {
                try
                {
                    os.close();
                }
                catch (Exception e)
                {
                }
               
            }
            
            if (c != null)
            {
                try
                {
                    c.close();
                }
                catch (Exception e)
                {
                }
            }            
            System.gc();
        }                        
    }
}
